package slidingWindow;

//https://leetcode.cn/problems/minimum-size-subarray-sum/
public class l_209 {
    public int minSubArrayLen(int target, int[] nums) {
        int n = nums.length, ans = n + 1, sum = 0, left = 0, right = 0;
        //滑动窗口维护和, >= target就左指针动, < target就右指针动
        while(right < n) {
            sum += nums[right++];
            if(sum >= target) ans = Math.min(ans, right - left + 1);
            //left <= right可不加, 防御性编程
            while(left <= right && sum >= target) {
                sum -= nums[left++];
                ans = Math.min(ans, right - left + 1);
            }
        }
        return ans == n + 1 ? 0 : ans;
    }
}
